package com.junyidao.game.controller;

import com.junyidao.game.constant.BaseConstant;
import com.junyidao.game.constant.OperLogConstant;
import com.junyidao.game.constant.SysUserConstant;
import com.junyidao.game.core.LoginInfo;
import com.junyidao.game.core.annotation.Action;
import com.junyidao.game.core.annotation.OperLog;
import com.junyidao.game.pojo.SysPriv;
import com.junyidao.game.pojo.SysUser;
import com.junyidao.game.pojo.TChannel;
import com.junyidao.game.service.ChannelService;
import com.junyidao.game.service.SysPrivService;
import com.junyidao.game.service.UserService;
import com.junyidao.game.vo.SysPrivVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zzh on 2016/7/2.
 */
@Controller
@RequestMapping("/user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private ChannelService channelService;

    @Autowired
    private SysPrivService sysPrivService;

    @RequestMapping
    @Action(desc = "进入账号管理页面")
    public ModelAndView list() {
        ModelAndView result = new ModelAndView("user/list");
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/list")
    @Action(desc = "获取账号管理列表", sysPriv = false)
    public Map<String, Object> listData(SysUser SysUser) {
        Map<String, Object> map = userService.getAll(SysUser);
        return resultSucess(map);
    }

    @ResponseBody
    @RequestMapping(value = "/activate")
    @Action(operType = OperLogConstant.USER_ACTIVATE, desc = "启用失效的账号", needLog = true)
    public Map<String, Object> activate(String uids) {
        userService.activate(uids, BaseConstant.STATUS_NORMAL);
        return resultSucess(null);
    }

    @ResponseBody
    @RequestMapping(value = "/disactivate")
    @Action(operType = OperLogConstant.USER_DISACTIVATE, desc = "注销启用的账号", needLog = true)
    public Map<String, Object> disactivate(String uids) {
        userService.activate(uids, BaseConstant.STATUS_DELETE);
        return resultSucess(null);
    }

    @RequestMapping(value = "/toAdd")
    @Action(desc = "进入新增账号页面")
    public ModelAndView toAdd() {
        ModelAndView result = new ModelAndView("user/add");
        List<TChannel> channelList = channelService.selectChannelBuUid(LoginInfo.getUser());
        result.addObject("channelList", channelList);
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/privList")
    @Action(desc = "获取权限列表", sysPriv = false)
    public Map<String, Object> privList(Integer uid) {
        Map<String, Object> map = new HashMap<>();
        SysUser user = LoginInfo.getUser();
        List<SysPrivVo> privList = sysPrivService.getUserRolePrivList(user.getUid());
        if (privList.size() == 0) {
            // 如果根据角色 没有权限，则根据uid获取
            privList = sysPrivService.getUserPrivList(user.getUid());
        }
        if (uid != null) {
            List<SysPriv> hasList = sysPrivService.getSysPrivList(uid);
            map.put("hasList", hasList);
        }
        map.put("list", privList);
        return resultSucess(map);
    }

    @ResponseBody
    @RequestMapping(value = "/add")
    @Action(operType = OperLogConstant.USER_ADD, desc = "新增账号", needLog = true)
    public Map<String, Object> add(SysUser sysUser, String channelIds, String privIds) {
        userService.addSysUser(sysUser, channelIds, privIds);
        return resultSucess(null);
    }

    @ResponseBody
    @RequestMapping(value = "/update")
    @Action(operType = OperLogConstant.USER_EDIT, desc = "编辑账号", needLog = true)
    public Map<String, Object> update(SysUser sysUser, String channelIds, String privIds) {
        userService.updateSysUser(sysUser, channelIds, privIds);
        return resultSucess(null);
    }

    @RequestMapping(value = "/detail")
    @Action(desc = "账号详情")
    public ModelAndView detail(Integer uid, String editFlag) {
        ModelAndView result = new ModelAndView("user/detail");
        SysUser user = userService.selectByPrimaryKey(uid);
        List<TChannel> channelList = channelService.selectChannelBuUid(LoginInfo.getUser());
        List<TChannel> hasChannelList = channelService.selectChannelBuUid(user);
        for (int i = 0; i < channelList.size(); i++) {
            TChannel c1 = channelList.get(i);
            for (int j = 0; j < hasChannelList.size(); j++) {
                TChannel c12 = hasChannelList.get(j);
                if (c1.getChannelId().equals(c12.getChannelId())) {
                    channelList.remove(i);
                }
            }
        }
        result.addObject("channelList", channelList);
        result.addObject("hasChannelList", hasChannelList);
        result.addObject("user", user);
        result.addObject("editFlag", editFlag);
        return result;
    }
}
